package com.cduestc.mylogin;

import static android.widget.RadioGroup.*;

import androidx.appcompat.app.AppCompatActivity;
import com.pojo.Login;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MyLoginActivity extends Activity {
	/** Called when the activity is first created. */

	public static String recodeHobby;
	public static String recodeSex = "保密";

	EditText user;
	EditText pwd;
	TextView aboutUs,forgetPwd;
	Button btn1,btn2,btnon;
	CheckBox pwdShow;
	Spinner autoLogin;
	SharedPreferences sharedPre,shareMsg;
	String flagTime = "";
	private static String sendU;
	private static String sendP;

	public static String getSendU() {
		return sendU;
	}

	public static void setSendU(String sendU) {
		MyLoginActivity.sendU = sendU;
	}

	public static String getSendP() {
		return sendP;
	}

	public static void setSendP(String sendP) {
		MyLoginActivity.sendP = sendP;
	}


	@Override
	public void onCreate(Bundle savedInstanceState) throws NullPointerException {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		System.out.println("start run");

		btn1 = (Button) this.findViewById(R.id.btn1);
		btn2 =(Button) this.findViewById(R.id.btn2);
		btnon = (Button) this.findViewById(R.id.btnon);

		pwdShow = (CheckBox) this.findViewById(R.id.pwdShow);
//		bindMessage = findViewById(R.id.bindMessage);
		aboutUs = this.findViewById(R.id.aboutUs);
		forgetPwd = this.findViewById(R.id.forgetPwd);
		autoLogin = this.findViewById(R.id.autoLogin);
		sharedPre = this.getSharedPreferences("myMessage",MODE_PRIVATE);
		shareMsg = this.getSharedPreferences("selfMsg",MODE_PRIVATE);

		user = (EditText) this.findViewById(R.id.user);
		pwd = (EditText) this.findViewById(R.id.pwd);

		//判断是否有保存信息
		Long nowLoginTime = System.currentTimeMillis();
		Long loseT = sharedPre.getLong("loseTime",0);
		if(nowLoginTime > loseT){
			SharedPreferences.Editor seClear = sharedPre.edit();
			seClear.clear();
			seClear.commit();
//			Toast.makeText(MyLoginActivity.this, "登录信息已失效", Toast.LENGTH_SHORT).show();
		}else {

			Intent inten1 = new Intent(MyLoginActivity.this,HomePage.class);
			startActivity(inten1);
//			String uname = sharedPre.getString("username", "");
//			String passwd = sharedPre.getString("password", "");
//
//			user.setText(uname);
//			pwd.setText(passwd);
		}

		//接收修改后的密码,并显示出来，密码修改后才起作用
		if(!PwdChange.keepPwd.equals("")){
			pwd.setText(PwdChange.keepPwd);
		}

		//登录
		btn1.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String u = user.getText().toString().trim();
				String p = pwd.getText().toString().trim();

				if(u.equals("") || p.equals("")){
					Toast.makeText(MyLoginActivity.this, "请先输入用户名和密码", Toast.LENGTH_SHORT).show();
				}else{
					long nowTime = System.currentTimeMillis();
					if(flagTime == "一分钟"){
						SharedPreferences.Editor seOneMin = sharedPre.edit();
						//现在时间 + 一分钟后的时间戳
						Long oneMin = nowTime + 1*60*1000;
						seOneMin.putLong("loseTime",oneMin);
						seOneMin.putString("username",u);
						seOneMin.putString("password",p);
						seOneMin.commit();
						Toast.makeText(MyLoginActivity.this, "保存时间已设置为一分钟", Toast.LENGTH_SHORT).show();
					}else if(flagTime == "一小时"){
						SharedPreferences.Editor seOneMin = sharedPre.edit();
						//现在时间 + 一小时后的时间戳
						Long oneHour = nowTime + 60*60*1000;
						seOneMin.putLong("loseTime",oneHour);
						seOneMin.putString("username",u);
						seOneMin.putString("password",p);
						seOneMin.commit();
						Toast.makeText(MyLoginActivity.this, "保存时间已设置为一小时", Toast.LENGTH_SHORT).show();
					}else if(flagTime == "一天"){
						SharedPreferences.Editor seOneDay = sharedPre.edit();
						Long oneDay = nowTime + 24*60*60*1000;
						seOneDay.putLong("loseTime",oneDay);
						seOneDay.putString("username",u);
						seOneDay.putString("password",p);
						seOneDay.commit();
						Toast.makeText(MyLoginActivity.this, "保存时间已设置为一天", Toast.LENGTH_SHORT).show();
					}else if(flagTime == "一周"){
						SharedPreferences.Editor seOneWeek = sharedPre.edit();
						Long oneWeek = nowTime + 7*24*60*60*1000;
						seOneWeek.putLong("loseTime",oneWeek);
						seOneWeek.putString("username",u);
						seOneWeek.putString("password",p);
						seOneWeek.commit();
						Toast.makeText(MyLoginActivity.this, "保存时间已设置为一周", Toast.LENGTH_SHORT).show();
					}else if(flagTime == "一个月"){
						SharedPreferences.Editor seOneMon = sharedPre.edit();
						Long oneMon = nowTime + 30*24*60*60*1000;
						seOneMon.putLong("loseTime",oneMon);
						seOneMon.putString("username",u);
						seOneMon.putString("password",p);
						seOneMon.commit();
						Toast.makeText(MyLoginActivity.this, "保存时间已设置为一个月", Toast.LENGTH_SHORT).show();
					}
//					else if(flagTime == "清除设置"){
//						SharedPreferences.Editor seC = sharedPre.edit();
//						seC.clear();
//						seC.commit();
//						Toast.makeText(MyLoginActivity.this, "已清除设置", Toast.LENGTH_SHORT).show();
//					}

					//登录跳转

					//清理修改后的密码的值
					PwdChange.keepPwd = "";

					sendU = u;
					sendP = p;

					Intent intentHome = new Intent(MyLoginActivity.this,HomePage.class);
					startActivity(intentHome);
				}
			}
		});


		//重置输入框
		btn2.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				user.setText("");
				pwd.setText("");
			}

		});

		//忘记密码
		forgetPwd.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				String ema = shareMsg.getString("email","");
				String pho = shareMsg.getString("phone","");
				if(!(ema.equals("") && pho.equals(""))){
					Intent intent = new Intent(MyLoginActivity.this, VerifyTheWay.class);
					startActivityForResult(intent, 1001);
				}else {
					Toast.makeText(MyLoginActivity.this,"您当前没有绑定任何信息",Toast.LENGTH_SHORT).show();
				}
			}
		});

		//用户协议
		aboutUs.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				String data="http://www.xinjingxiang.com/pro.htm";
				Uri uri = Uri.parse(data);
				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_VIEW);
				intent.setData(uri);
				startActivity(intent);
			}
		});

		String[] timeChoose = {"请选择","一分钟","一小时","一天","一周","一个月"};
		ArrayAdapter<String> aadapter = new ArrayAdapter<String>(MyLoginActivity.this, android.R.layout.simple_spinner_item,timeChoose);
		aadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		autoLogin.setSelection(0);
		autoLogin.setAdapter(aadapter);
		//选择自动登陆时间
		autoLogin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
				String res = adapterView.getItemAtPosition(i).toString();
				flagTime = res;
			}

			@Override
			public void onNothingSelected(AdapterView<?> adapterView) {

			}
		});

		//注册
		btnon.setOnClickListener(new OnClickListener(){
			boolean flag = false;

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				//
				AlertDialog.Builder builder = new AlertDialog.Builder(MyLoginActivity.this,AlertDialog.THEME_HOLO_LIGHT);
				//获取布局
				View view1 = View.inflate(MyLoginActivity.this,R.layout.dialog, null);

				//final Button btnlogon;
				EditText username;
				EditText password1;
				EditText password2;
				CheckBox pwdCheckBox;
				RadioGroup rgSex;
				RadioButton rg1,rg2,rg3;
				CheckBox cbfoot,cbbasket,cbtennis;
				SeekBar seekbar;

				//btnlogon = (Button) view1.findViewById(R.id.btnlogon);
				username = (EditText) view1.findViewById(R.id.username);
				password1 = (EditText) view1.findViewById(R.id.password1);
				password2 = (EditText) view1.findViewById(R.id.password2);
				pwdCheckBox = (CheckBox) view1.findViewById(R.id.pwdCheckBox);
				rgSex =  (RadioGroup) view1.findViewById(R.id.rgSex);
				rg1 = view1.findViewById(R.id.rg1);
				rg2 = view1.findViewById(R.id.rg2);
				rg3 = view1.findViewById(R.id.rg3);

				cbfoot = view1.findViewById(R.id.cbfoot);
				cbbasket = view1.findViewById(R.id.cbbasket);
				cbtennis = view1.findViewById(R.id.cbtennis);
				seekbar = view1.findViewById(R.id.seekbar);

				rg1.setChecked(true);
				//设置参数
				builder.setIcon(android.R.drawable.star_big_on).setTitle("注册").setView(view1);

				//方案三：创建PositiveButton，但是不设置点击事件，后面实现点击事件监控
				builder.setPositiveButton("注册", null);

				//显示或隐藏密码
				pwdCheckBox.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						if(pwdCheckBox.isChecked()){
							password1.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
							password2.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
						}else{
							password1.setTransformationMethod(PasswordTransformationMethod.getInstance());
							password2.setTransformationMethod(PasswordTransformationMethod.getInstance());
						}
					}
				});

				//设置性别
				rgSex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						// TODO Auto-generated method stub
						if(checkedId == rg1.getId()){
							recodeSex = "保密";
						}else if(checkedId == rg2.getId()){
							recodeSex = "男";
						}else if(checkedId == rg3.getId()){
							recodeSex = "女";
						}
					}
				});

				//注册验证条
				seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

					public void changeColor(){
						if(flag == true){
							seekbar.setProgressDrawable(getDrawable(R.drawable.progressdrawable2));
						}else{
							seekbar.setProgressDrawable(getDrawable(R.drawable.progressdrawable));
						}
					}
					@Override
					public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
						if(seekbar.getProgress() == seekBar.getMax()){
							flag = true;
							changeColor();
						}else{
							flag = false;
							changeColor();
						}

					}

					@Override
					public void onStartTrackingTouch(SeekBar seekBar) {

					}
					@Override
					public void onStopTrackingTouch(SeekBar seekBar) {
						if(seekbar.getProgress() == seekBar.getMax()){
							flag = true;
							changeColor();
						}else{
							flag = false;
							changeColor();
						}
					}
				});

				final AlertDialog dialog = builder.create();
				dialog.show();

				dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {

						// TODO Auto-generated method stub
						String su = username.getText().toString().trim();
						String sp1 = password1.getText().toString().trim();
						String sp2 = password2.getText().toString().trim();
						String hobby = "";

						if(cbfoot.isChecked()) {
							hobby = hobby + "足球" + ",";
						}
						if(cbbasket.isChecked()) {
							hobby = hobby +"篮球" + ",";
						}
						if(cbtennis.isChecked()) {
							hobby = hobby +"乒乓球";
						}

						recodeHobby = hobby;

						if(!su.equals("") && !sp1.equals("") && !sp2.equals("")){

							if(su.length() < 2){
								Toast.makeText(MyLoginActivity.this,"账号长度不能小于两位",Toast.LENGTH_SHORT).show();
								return;
							}else if(sp1.length() < 6){
								Toast.makeText(MyLoginActivity.this, "密码长度不能小于6位", Toast.LENGTH_SHORT).show();
								return;
							}else if(!sp1.equals(sp2)){
								Toast.makeText(MyLoginActivity.this,"两次输入密码不一致",Toast.LENGTH_SHORT).show();
								password1.setText("");
								password2.setText("");
								return;
							}else{
								//判断滑动条
								if(flag == false){
									Toast.makeText(MyLoginActivity.this, "请拖动滑块验证", Toast.LENGTH_SHORT).show();
									return;
								}
								Toast.makeText(MyLoginActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
								user.setText(su);
								pwd.setText(sp1);
								dialog.dismiss();
							}
						}else{
							Toast.makeText(MyLoginActivity.this, "账号，密码均不能为空", Toast.LENGTH_SHORT).show();
						}
					}});
			}
		});

		//设置密码可见
		pwdShow.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(pwdShow.isChecked()){
					pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
				}else{
					pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
				}
			}

		});

	}
}